package com.gzxx.util;

import com.gzxx.constant.ServiceCodeStatus;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;

/**
 * @author MAX
 * @date 2020/7/23 10:06 上午
 * 服务端统一返回
 *
 */

@Getter
@ApiModel(value="服务端返回实体",description="服务端返回实体")
public class ServerResponse<T> {

    @ApiModelProperty(value = "系统code", required = true, example = "0")
    private int code;

    @ApiModelProperty(value = "系统消息", required = true, example = "操作成功")
    private String msg;

    @ApiModelProperty(value = "返回数据", required = false, example = "{xx:xx,xx:xx}")
    private T data;

    public ServerResponse(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static ServerResponse success() {
        return success("操作成功");
    }


    public static ServerResponse success(int code) {
        return success(code,"操作成功", null);
    }

    public static ServerResponse success(int code, String msg) {
        return success(code,msg, null);
    }

    public static ServerResponse success(Object data) {
        return success(ServiceCodeStatus.SUCCESS,"操作成功", data);
    }

    public static ServerResponse success(int code, String msg, Object data) {
        return new ServerResponse(code, msg, data);
    }

    public static ServerResponse error() {
        return error("操作失败");
    }

    public static ServerResponse error(String msg) {
        return error(ServiceCodeStatus.FAILURE, msg);
    }

    public static ServerResponse error(int code, String msg) {
        return new ServerResponse(code, msg, null);
    }
}
